package com.wuwei.jy.controller;

import com.wuwei.jy.Service.ArticleService;
import com.wuwei.jy.Service.NewsSv;
import com.wuwei.jy.domain.g.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private ArticleService articleService;

    private int limit = 5;

    @RequestMapping("/test")
    public String home() {

        return "/admin/test";

    }

    @RequestMapping(value = "/menu", produces = "application/json;charset=UTF-8")
    public String menu() {
        return "/admin/menu";
    }

    @RequestMapping(value = "/footer")
    public String footer() {
        return "/admin/footer";
    }

    @RequestMapping(value = "/article")
    public String article() {
        return "/admin/article";
    }

    @RequestMapping(value = "/articleList")
    public String articleList(int page,Model model) {
        List<Article> articleList = articleService.getArticleList((page - 1) * limit,limit);
        model.addAttribute("articleList",articleList);
        model.addAttribute("page",page);
        long pageCount = articleService.getArticleListCount();
        if (pageCount % limit > 0)
            model.addAttribute("articleListCount",pageCount/limit + 1);
        else
            model.addAttribute("articleListCount",pageCount/limit);
        return "/admin/article_list";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String Login() {

        return "/admin/login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String Login(String username, String pwd, HttpServletResponse response, HttpServletRequest request, Model model) {

        if (username.equals("admin") && pwd.equals("feizhougaici")) {

            request.getSession().setAttribute("admin", "1");
            return "redirect:/admin/test";
        } else {
            model.addAttribute("msg", "登录失败");
            return "/admin/login";
        }


    }

    @RequestMapping(value = "/loginOut", method = RequestMethod.GET)
    public String loginOut(HttpServletRequest request) {
        request.getSession().setAttribute("admin", "1");
        return "redirect:/admin/login";
    }


    @RequestMapping(value = "/addArticle")
    public String addArticle(String title,String content,String img,int type,String endDate,String wechatName,String wechatUrl,int weighNumber) {
        articleService.addArticle(title,content,img,type,endDate,wechatName,wechatUrl,weighNumber);
        return "/admin/article";
    }


    @RequestMapping(value = "/deleteArticle")
    public String deleteArticle(int id,int page,Model model) {
        articleService.deleteArticle(id);
        articleList(page,model);
        return "/admin/article_list";
    }

    @RequestMapping(value = "/articleInfo")
    public String articleInfo(int id,Model model) {
        Article article = articleService.articleInfo(id);
        model.addAttribute("article",article);
        if (article.getTemplet() != null && !article.getTemplet().equals("")){
            if (article.getTemplet().equals("")){
                return "/admin/" + article.getTemplet();
            }else{
                return "/admin/" + article.getTemplet();
            }
        } else {
            return "/admin/article_info1";
        }
    }



}
